//
//  HTMusicViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTMusicViewController.h"
#import "PKRequestManager.h"
#import "HTClassifyModel.h"
#import "HTMusicMainCell.h"
#import "UIImageView+WebCache.h"
#import "HTSingerViewController.h"

#define kURL @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%252C2&f=f320&s=s310&imsi=&hid=&splus=8.1.2&active=1&net=2&openudid=6520cfef5c2a548f91e7a2ef2bbb4dcbd65c20a9&idfa=FE06BD4D-0645-40B5-9A51-27E13F83A35E&utdid=VdWOeXwUZRwDAA9QVJCvEyX9&alf=201200&bundle_id=com.ttpod.music"

@interface HTMusicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation HTMusicViewController
- (NSMutableArray *)dataSourceArray {
    
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH - 10, 50);
    layout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH - 10, 50);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset =  UIEdgeInsetsMake(10, 10, 10, 10);
    _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 150) collectionViewLayout:layout];
    _mainCollectionView.backgroundColor = [UIColor clearColor];
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    [_mainCollectionView registerClass:[HTMusicMainCell class] forCellWithReuseIdentifier:@"cell"];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 150)];
    img.image = [UIImage imageNamed:@"沙滩"];
    [self.view addSubview:img];
    
    [self.view addSubview:_mainCollectionView];
    [self configureData];
}

- (void)configureData {
    
    [PKRequestManager requestWithType:GET urlString:kURL parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *modelDic in dict[@"data"]) {
            HTClassifyModel *model = [[HTClassifyModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainCollectionView reloadData];
    } error:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HTMusicMainCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HTClassifyModel *model = _dataSourceArray[indexPath.item];
    [cell.cellImgView sd_setImageWithURL:[NSURL URLWithString:model.pic_url]];
    cell.titleLabel.text = model.title;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((SCREEN_WIDTH -40)/3, (SCREEN_WIDTH -40)/3+(SCREEN_WIDTH -40)/6);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HTSingerViewController *singerVC = [[HTSingerViewController alloc] init];
    HTClassifyModel *model = _dataSourceArray[indexPath.row];
    singerVC.id_name = model.id_name;
    singerVC.nameTitle = model.title;
    [self.navigationController pushViewController:singerVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
